package logic

import (
	"box-fresh/boxfreshrpc/boxfreshrpc"
	"box-fresh/boxfreshrpc/internal/svc"
	"box-fresh/model/mysql"
	"context"
	"errors"

	"github.com/zeromicro/go-zero/core/logx"
)

type ShopAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewShopAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShopAddLogic {
	return &ShopAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 商品添加
func (l *ShopAddLogic) ShopAdd(in *boxfreshrpc.ShopAddRequest) (*boxfreshrpc.ShopAddResponse, error) {
	// todo: add your logic here and delete this line

	var shop mysql.Shop
	mysql.DB.Where("name=?", in.Name).First(&shop)
	if shop.ID != 0 {
		return nil, errors.New("该商品已存在，不能重复添加")
	}

	shop = mysql.Shop{
		Name:  in.Name,
		Style: in.Style,
		Price: float64(in.Price),
	}

	err := shop.ShopAdd(mysql.DB)
	if err != nil {
		return nil, err
	}

	return &boxfreshrpc.ShopAddResponse{
		ShopId: int64(shop.ID),
	}, nil
}
